package org.hackystat.app.projectstatus.data;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletRequest;

import org.hackystat.core.common.project.Project;
import org.hackystat.core.common.project.ProjectManager;
import org.hackystat.core.kernel.admin.ServerProperties;
import org.hackystat.core.kernel.user.User;
import org.hackystat.core.kernel.util.Day;

/**
 * Implements the ability to generate HTML Emails
 * which are gathered for the moment from static
 * HTML pages.
 * 
 * @author Josh Stupplebeen
 *
 */
public class ProjectStatusHTMLMailer {

	/** Daily Project Data Manager. */
	private static ProjectStatusDataManager manager;

	/**
	 * Separates the sending of the HTML Email from
	 * returning the appropriate JSP page.
	 * 
	 * @param request The HttpServletRequest page.
	 * @param user The User logged in.
	 * @param addrs The list of email addresses.
	 * @param project The project these emails are associated with.
	 * @param day the Day for which this email is associated.
	 * @return A boolean indicating whether email sending was successful.
	 */
	public boolean processHTMLMail(HttpServletRequest request, User user,
			List<String> addrs, Project project, Day day) {

		//Test Code

		boolean emailSent = false;

		try {
			emailSent = sendHTMLMail(user, addrs, project, day);
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException");
		} catch (IOException e) {
			System.out.println("IOException");
		}
		return emailSent;
	}

	/**
	 * Instantiates the formatting of the emails to 
	 * accept HTML as the body of the emails.
	 * 
	 * @param user The User logged in.
	 * @param toAddr A list of strings of email addresses.
	 * @param project The project these emails are associated with.
	 * @param day The day which this email is associated.
	 * @return A boolean indicating email sending was successful.
	 * @throws IOException Thrown if file input was not successful.
	 * @throws FileNotFoundException Thrown if the HTML file is not found.
	 */
	public boolean sendHTMLMail(User user, List<String> toAddr,
			Project project, Day day) throws FileNotFoundException, IOException {

		manager = ProjectStatusDataManager.getInstance();

		Properties props = System.getProperties();
		props.put("mail.smtp.host", "mail.hawaii.edu");
		Session session = Session.getInstance(props, null);
		Message message = new MimeMessage(session);

		String adminEmail = "Hackystat Administrator <"
				+ ServerProperties.getInstance().getAdminEmail() + ">";

		BodyPart bphtml = new MimeBodyPart();
		Multipart mp = new MimeMultipart("alternative");
		ProjectManager pManager = ProjectManager.getInstance();
		Set<Project> projects = pManager.getProjectsWithOwner(user);
		String htmlEmailBody = "";

		List<String> emailBodies = new ArrayList<String>();

		emailBodies.add(manager.getHTMLPage(user, project, day));

		ServerProperties serverProperties = ServerProperties.getInstance();
		for (String s : toAddr) {
			if (serverProperties.isTestInstallation()
					|| s.endsWith(serverProperties.getTestDomain())) {
				return true;
			}
		}
		try {

			message.setSubject("HTML Emailer Test");
			message.setFrom(new InternetAddress(adminEmail));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"jstupple@hawaii.edu"));

			for (String s : emailBodies) {
				bphtml.setContent(s, "text/html");
				mp.addBodyPart(bphtml);

				message.setContent(mp);
			}
			Transport.send(message);

			return true;
		} catch (MessagingException mex) {
			System.out.println("Messaging Exception has been logged");
			ServerProperties.getInstance().getLogger().warning(
					"Mail failure to: " + toAddr + "\n" + mex);
			return false;
		}

	}

}
